package com.skins
{
	import flash.display.BitmapData;
	import flash.events.EventDispatcher;
	/**
	 * 图片资源库，使用图片路径为ID
	 * @author WZH(shch8.com)
	 */
	public class BitmapDataPool extends EventDispatcher
	{
		private static var Instace:BitmapDataPool=new BitmapDataPool;
		public static function getInstance():BitmapDataPool {
			return Instace;
		}
		
		private var idPool:Array;
		private var bdPool:Array;
		
		private var oldGetID:String;
		private var oldGetBD:BitmapData;
		
		public function BitmapDataPool() 
		{
			clear();
			
		}
		/**
		 * 位图加入缓存
		 * @param	id
		 * @param	bd
		 */
		public function push(id:String, bd:BitmapData):void
		{
			//if(bd!=null)trace(bd.rect)
			//trace("push:",id)
			var index:int = idPool.indexOf(id)
			if (index != -1) {
				if (bdPool[index] == null) {
					bdPool[index] = bd;
					return;
					}else {
						return;
					}
				}
			idPool.push(id);
			bdPool.push(bd);
		}
		/**
		 * 
		 * @param	id
		 * @return
		 */
		public function getBitmapData(id:String):BitmapData
		{
			//if(oldGetID==id)return oldGetBD;
			var index:int = idPool.indexOf(id);
			//trace("get:",index,id)
			if (index == -1) return null;
			oldGetID=id;
			oldGetBD=bdPool[index] as BitmapData
			return oldGetBD;
		}
		/**
		 * 通过ID移除缓存
		 * @param	id
		 * @return
		 */
		public function remove(id:String):Boolean
		{
			var index:int = idPool.indexOf(id);
			if (index == -1) return false;
			idPool.splice(index, 1);
			bdPool.splice(index, 1);
			return true;
		}
		/**
		 * 检测位图是否存在
		 * @param	id
		 * @return
		 */
		public function isExist(id:String):Boolean
		{
			return idPool.indexOf(id)!=-1;
		}
		/**
		 * 是否正在加载
		 * @param	id
		 * @return
		 */
		public function isLoading(id:String):Boolean
		{
			var index:int = idPool.indexOf(id);
			if (index != -1) {
				if (bdPool[index] == null) {
					return true;
					}
			}
			return false;
		}
		/**
		 * 缓存清除
		 */
		public function clear():void
		{
			idPool = [];
			bdPool = [];
		}
		
	}

}